Problem Challenge 3
We'll cover the following
Frequency Stack (hard) #
Design a class that simulates a Stack data structure, implementing the following two operations:
push(int num)
: Pushes the number ‘num’ on the stack.pop()
: Returns the most frequent number in the stack. If there is a tie, return the number which was pushed later.
Example:
After following push operations: push(1), push(2), push(3), push(2), push(1), push(2), push(5)
1. pop() should return 2, as it is the most frequent number
2. Next pop() should return 1
3. Next pop() should return 2
Try it yourself #
Try solving this question here: